package com.glq.book.service;

import com.glq.book.dao.BookDao;
import com.glq.book.dao.BorrowDao;
import com.glq.book.entity.Borrow;
import com.glq.book.vo.BorrowVO;

import java.util.List;

public class BorrowService {

    private BorrowDao borrowDao = new BorrowDao();
    private BookDao bookDao = new BookDao();

    /**
     * 添加借书记录
     * 先减库存，再添加书籍
     *
     * @param bookName
     * @param username
     * @param bookId
     * @return
     */
    public boolean insertBorrow(String bookName, String username, Integer bookId) {
        BorrowVO borrow = new BorrowVO(bookName, username);
        boolean subBookNumber = bookDao.subBookNumber(bookId, 1);
        if (!subBookNumber) {
            return !borrowDao.insertBorrow(borrow);
        }
        return false;
    }

    /**
     * 还书
     *
     * @param id
     * @param bookName
     * @return
     */
    public boolean updateBorrow(Integer id, String bookName) {
        boolean subBookNumber = bookDao.addBookNumber(bookName, 1);
        if (!subBookNumber) {
            return !borrowDao.updateBorrow(id);
        }
        return false;
    }

    /**
     * 借书记录（用户）
     *
     * @param username
     * @return
     */
    public List<Borrow> selectBorrowListByUser(String username) {
        return borrowDao.selectBorrowListByUser(username);
    }

    /**
     * 借书记录（管理员）
     *
     * @return
     */
    public List<Borrow> selectBorrowList() {
        return borrowDao.selectBorrowList();
    }
}
